﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Text;
using PowerCreator.LMS.Web.User;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Web.Course;
using PowerCreator.LMS.Course.Entity;
using PowerCreator.LMS.Web.Catalog;
using PowerCreator.LMS.ViewQuery;

public partial class sysadmin_UserCouseManager_ManagerUserCourse : PowerCreator.LMS.Web.Base.BasePage
{
    protected Course opCourse = new Course();
    Catalog opCatalog = new Catalog();
    ViewQuery opViewQuery = new ViewQuery();
    CatalogCourse opCatalogCourse = new CatalogCourse();
    Department opDepart = new Department();

    private string _SQL_Catalog_Course = "select distinct * from VIEW_UserCourseList ";
    private PowerCreator.LMS.Web.DL.User.LoginEntity login = new PowerCreator.LMS.Web.DL.User.LoginEntity();
    private string url = "../login.aspx";
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitPage();
            ViewState["OrderBy"] = "";      //排序字段
            ViewState["OrderType"] = " desc";//排序方式

            InitPage();

            string search = GetSearch();
            BinderPager(search);
            BinderRPT(search, 1);
            PreAspNetPager(AspNetPager1);
        }
    }
    protected override void InitPage()
    {
        this.Title = GetLanageValueByKey("SelCourse_lbl_title", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_InfoTitle.Text = GetLanageValueByKey("SelCourse_lbl_title", PowerCreator.LMS.Language.LanagetItemType.Page);
        //chkAll.Text = GetLanageValueByKey("chkbox_ManagerQuestion_chkboxAll", PowerCreator.LMS.Language.LanagetItemType.Page);
        //chkCancel.Text = GetLanageValueByKey("chkbox_ManagerQuestion_chkboxCancel", PowerCreator.LMS.Language.LanagetItemType.Page);
        //btnLock.Text = GetLanageValueByKey("btnAuditing", PowerCreator.LMS.Language.LanagetItemType.Page);

        
        btn_SortSearch.Text = GetLanageValueByKey("btn_Search", PowerCreator.LMS.Language.LanagetItemType.Page);

        base.InitPage();
    }
    //protected string GetCourseStateByData(int ucState, int courseType)
    //{
    //    if (courseType == 3)
    //    {
    //        return "--";
    //    }
    //    else if (ucState == 1)
    //    {
    //        return GetLanageValueByKey("State_Msg2", PowerCreator.LMS.Language.LanagetItemType.Page);
    //    }
    //    else if (ucState == 0)
    //    {
    //        return GetLanageValueByKey("AddCourseInfo_db_d2", PowerCreator.LMS.Language.LanagetItemType.Page);
    //    }
    //    else
    //    {
    //        return GetLanageValueByKey("AddCourseInfo_db_d2", PowerCreator.LMS.Language.LanagetItemType.Page);
    //    }
    //}


    private string GetSearch()
    {
        StringBuilder sbSearch = new StringBuilder();
        sbSearch.Append(_SQL_Catalog_Course);

        if (!string.IsNullOrEmpty(txt_SearchKey.Text))
        {
            sbSearch.Append(string.Format(" where CourseName like '%{0}%' or UserName like '%{0}%' or RealName like '%{0}%' ", txt_SearchKey.Text.Trim()));
        }
        return sbSearch.ToString();
    }

    private void BinderPager(string search)
    {
        AspNetPager1.PageSize = LMSPageSize;
        AspNetPager1.RecordCount = opViewQuery.Count(search);
    }
    private void BinderRPT(string search, int pageIndex)
    {
        string orderby = ViewState["OrderBy"].ToString();
        if (!string.IsNullOrEmpty(orderby))
        {
            search += " order by " + orderby + ViewState["OrderType"].ToString();
        }
        DataTable dt = opViewQuery.Query(search,LMSPageSize, pageIndex);
        rptCourse.DataSource = dt;
        rptCourse.DataBind();
    }

    //protected void chkAll_CheckedChanged(object sender, EventArgs e)
    //{
    //    chkCancel.Checked = false;
    //    //全选/取消
    //    for (int i = 0; i < rptCourse.Items.Count; i++)
    //    {
    //        CheckBox cbox = (CheckBox)rptCourse.Items[i].FindControl("chkSelect");
    //        if (chkAll.Checked)
    //            cbox.Checked = true;
    //        else
    //            cbox.Checked = false;
    //    }
    //}
    //protected void chkCancel_CheckedChanged(object sender, EventArgs e)
    //{
    //    chkAll.Checked = false;
    //    //反选
    //    for (int i = 0; i < rptCourse.Items.Count; i++)
    //    {
    //        CheckBox cbox = (CheckBox)rptCourse.Items[i].FindControl("chkSelect");
    //        if (cbox.Checked)
    //            cbox.Checked = false;
    //        else
    //            cbox.Checked = true;
    //    }
    //}

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderRPT(search, AspNetPager1.CurrentPageIndex);
    }

    //protected void rptCourse_ItemCommand(object source, RepeaterCommandEventArgs e)
    //{
    //    if (e.CommandName.ToLower() == "leditor")
    //    {
    //        PowerCreator.LMS.Web.Course.UserCourse objUserCourseManager = new PowerCreator.LMS.Web.Course.UserCourse();
    //        PowerCreator.LMS.Course.Entity.UserCourse vo = objUserCourseManager.LoadCourseManager(int.Parse(e.CommandArgument.ToString()));
            
    //        vo.UCState = 1;

    //        if (vo != null)
    //        {
    //            objUserCourseManager.UpdateUserCourse(vo);
    //        }

    //        //Script.Alert(this, "", GetLanageValueByKey("msg_CelueSelectQuest_shuruti23", PowerCreator.LMS.Language.LanagetItemType.Page));
    //        string search = GetSearch();
    //        BinderRPT(search, AspNetPager1.CurrentPageIndex);
    //    }
    //    else if (e.CommandName == "orderby")
    //    {
    //        ViewState["OrderBy"] = e.CommandArgument.ToString();
    //        string search = GetSearch();
    //        BinderRPT(search, AspNetPager1.CurrentPageIndex);
    //        if (ViewState["OrderType"].ToString() == " desc")
    //            ViewState["OrderType"] = " asc";
    //        else
    //            ViewState["OrderType"] = " desc";
    //    }
    //}


    //protected void btnLock_Click(object sender, EventArgs e)
    //{
    //    int count = 0;
    //    try
    //    {
    //        for (int i = 0; i < rptCourse.Items.Count; i++)
    //        {
    //            CheckBox cbox = (CheckBox)rptCourse.Items[i].FindControl("chkSelect");
    //            if (cbox.Checked)
    //            {
    //                count += 1;
    //                Label lblid = (Label)rptCourse.Items[i].FindControl("lblid");
    //                PowerCreator.LMS.Web.Course.UserCourse objUserCourseManager = new PowerCreator.LMS.Web.Course.UserCourse();
    //                PowerCreator.LMS.Course.Entity.UserCourse vo = objUserCourseManager.LoadCourseManager(int.Parse(lblid.Text)); ;
                   
    //                vo.UCState = 1;
    //                if (vo != null)
    //                {
    //                    objUserCourseManager.UpdateUserCourse(vo);
    //                }
                    
    //            }
    //        }
    //        if (count == 0)
    //        {
    //            Script.Alert(this, "", GetLanageValueByKey("AddPaper_msg22", PowerCreator.LMS.Language.LanagetItemType.Page));
    //            return;
    //        }
    //        Script.Alert(this, "", GetLanageValueByKey("msg_CelueSelectQuest_shuruti23", PowerCreator.LMS.Language.LanagetItemType.Page));
    //    }
    //    catch
    //    {
    //        Script.Alert(this, "", GetLanageValueByKey("msg_CelueSelectQuest_shuruti24", PowerCreator.LMS.Language.LanagetItemType.Page));
    //    }
    //    chkAll.Checked = false;
    //    chkCancel.Checked = false;

    //    string search = GetSearch();
    //    BinderRPT(search, AspNetPager1.CurrentPageIndex);
    //}
    protected void btn_SortSearch_Click(object sender, EventArgs e)
    {

        string search = GetSearch();
        BinderPager(search);
        BinderRPT(search, AspNetPager1.CurrentPageIndex);
    }
}
